import {
  apiUrl,
  apiUrlOcr,
  wxType
} from './env';

function jfeRequest(
  url,
  data,
  method,
  OCR
) {
  return new Promise((resolve, reject) => {
    url = OCR ? apiUrlOcr + url : apiUrl + url;
    // url = apiUrl + url;
    const user = wx.getStorageSync('user');
    data.operator = user.openid
    for (const key in data) {
      if (data[key] === null || data[key] === undefined) {
        data[key] = '';
      }
    }
    if (method === 'GET') {
      url += '?';
      for (const key in data) {
        if (data.hasOwnProperty(key)) {
          url += key + '=' + data[key] + '&';
        }
      }
      url = url.substring(0, url.length - 1);
    }
    // console.log('请求接口', url, data);
    wx.request({
      url,
      data,
      method,
      header: {
        Authorization: wx.getStorageSync('token'),
        // wechatToken: 'eyJhbGciOiJFUzI1NiJ9.eyJqdGkiOiIxMjMiLCJzdWIiOiJhYmMiLCJ1c2VySWQiOiJhZG1pbiIsInNlc3Npb25JZCI6IkVGN0FDMDM0MzkxRDc3NzMzMTYwMjc5QUE0Mjk3QTI5IiwiZXhwaXJlVGltZSI6MTcxNDg5MDA3OTUxNywicmVmcmVzaFRpbWUiOjE3MTU5NjgyNzk1MTd9.a5n-lg5NCZvPeb3ePSW2nxfmuwZ34IyKZcHkvri1Ah__NCuk5FKpX_Qgt5WqsSaa2bX-TnGLUc1fqqw8u4Q8hQ'
      },
      success(res) {
        wx.hideLoading();
        let data = res.data;
        const header = res.header;
        if (header.Authorization) {
          wx.setStorageSync('token', header.Authorization);
        }
        // console.log(data)
        if (!data.isSuccess) {

          if (data.statusCode !== '200') {
            wx.showToast({
              title: data.message,
              icon: 'none',
              duration: 5000,
            });
            // console.log('接口错误', url, data);
          } else {
            try {
              wx.login({
                async success(res) {
                  const code = res.code;
                  api
                    .login({
                      code,
                      wxType,
                      sysCode: 'BranchOrg',
                      applyType: '2',
                    })
                    .then((loginRes) => {
                      wx.setStorageSync('token', loginRes.data.token);
                      wx.setStorageSync('userInfo', loginRes.data.user);
                      wx.showToast({
                        title: '登录超时请重试',
                        icon: 'none',
                        duration: 5000,
                      });
                    })
                    .catch((e) => {
                      if (e.code === '403') {
                        wx.redirectTo({
                          url: '/pages/index/index',
                        });
                      } else {
                        console.log('loginError', e);
                      }
                    });
                },
                fail(e) {
                  console.log(e);
                },
              });
            } catch (e) {}
          }
          reject(data);
        } else {
          wx.showToast({
            title: data.message,
            icon: 'success',
            duration: 5000,
          });
          // console.log('接口成功', url, data);
          resolve(data);
        }
      },
      fail(err) {
        wx.hideLoading();
        reject(err);
      },
    });
  });
}
const api = {
  /**
   * 登录
   */
  login: (data) => {
    return jfeRequest('/gjssmj/appapi/common/bindWechat', data, 'POST');
  },
  //小程序数据字典接口
  getDictData: (data) => {
    return jfeRequest('/gjssmj/appapi/common/getDictData/' + data.dictCode, {}, 'POST');
  },
  //客车货车查询
  carOrderList: (data) => {
    return jfeRequest('/gjssmj/appapi/common/carOrderList', data, 'POST');
  },
  //客车货车新增
  carOrderAdd: (data) => {
    return jfeRequest('/gjssmj/appapi/common/carOrderAdd', data, 'POST');
  },
  //访客查询
  applyBatchWeChatList: (data) => {
    return jfeRequest('/gjssmj/appapi/common/applyBatchWeChatList', data, 'POST');
  },
  //访客新增
  applyBatchAddWeChat: (data) => {
    return jfeRequest('/gjssmj/appapi/common/applyBatchAddWeChat', data, 'POST');
  },
  //访客详情
  applyBatchWeChatDetials: (data) => {
    return jfeRequest('/gjssmj/appapi/common/applyBatchWeChatDetials', data, 'POST');
  },
  //机构查询
  orgTreeTree: (data) => {
    return jfeRequest('/gjssmj/appapi/common/orgTreeTree', data, 'POST');
  },
  //收货确认接口
  receiptConfirm: (data) => {
    return jfeRequest('/gjssmj/appapi/common/receiptConfirm', data, 'POST');
  },
  //原板货车申请接口
  rowBoardCarAdd: (data) => {
    return jfeRequest('/gjssmj/appapi/common/rowBoardCarAdd', data, 'POST');
  },
  //CR文字识别
  discerniWords: (data) => {
    return jfeRequest('/ui/upload_pic', data, 'POST', 'OCR');
  },
  //钢卷号查询车号
  getCarNo: (data) => {
    return jfeRequest('/gjssmj/appapi/common/getCarNo', data, 'POST');
  },
  //扫一扫钢卷号查询
  getOccupy: (data) => {
    return jfeRequest('/gjssmj/appapi/common/getOccupy', data, 'POST');
  },
  //是否已绑定
  queryByOpenId: (data) => {
    return jfeRequest('/gjssmj/appapi/common/queryByOpenId', data, 'POST');
  },
  //客车货车详情
  carOrderDetials: (data) => {
    return jfeRequest('/gjssmj/appapi/common/carOrderDetials', data, 'POST');
  },
  //车牌绑定
  bindCarNo: (data) => {
    return jfeRequest('/gjssmj/appapi/common/bindCarNo', data, 'POST');
  },
  //车牌查询
  getByOpenId: (data) => {
    return jfeRequest('/gjssmj/appapi/common/getByOpenId', data, 'POST');
  },
  //钢卷号模糊查询
  queryCoil: (data) => {
    return jfeRequest('/gjssmj/appapi/common/queryCoil', data, 'POST');
  },
    //人员二维码
    visitor: (data) => {
      return jfeRequest('/gjssmj/appapi/common/qr/visitor', data, 'POST');
    },
};
export default api;